
import $ from 'jquery'
export default ({
    state: {
        id: "",
        username: "",
        majorid: "",
        token: "",
        is_login: false, //登录状态
    },
    getters: {
    },
    mutations: {
        updateUser(state, user) {
            state.id = user.id;
            state.username = user.username;
            state.majorid = user.majorid;
            state.is_login = user.is_login;
        },
        updateToken(state, token) {
            state.token = token;
        },
        logout(state) {
            state.id = "";
            state.username = "";
            state.majorid = "";
            state.is_login = false;
            state.token = "";
        }
    },
    actions: {
        login(context, data) {
            $.ajax({
                url: "http://10.142.225.1:3000/user/account/token",
                type: "post",
                data: {
                    username: data.username,
                    password: data.password
                },
                success(resp) {
                    if (resp.message == "Success!") {
                        context.commit("updateToken", resp.token);
                        console.log("登录成功");
                        data.success(resp);//回调函数
                    }
                    else {
                        data.error(resp);
                    }
                },
                error: function (xhr, _status, error) {
                    console.log("AJAX 请求失败");
                    console.log("状态码：" + xhr.status);
                    console.log("错误信息：" + error);
                }
            })
        },
        logout(context) {
            context.commit("logout");
        },
        /*返回用户信息
        getinfo(context, data) {
            $.ajax({
                url: "http://10.142.225.1:3000/user/account/info",

            })
        }*/
    },
    modules: {
    }
})
